import { DEVICE_TYPE, deviceType } from "@/utils/chat";
import { Request, app_baseUrl } from "@/utils/request";
import Taro from "@tarojs/taro";

export interface IToFeedBackParams {
  /**
   * @feedback 类型 thumbsUp: 赞成 ,thumbsDown :反对
   */
  feedback: "thumbsUp" | "thumbsDown";
  /**
   * @id 操作对话 id
   */
  id: string;
}

interface IFeedBackParams extends IToFeedBackParams {
  sessionId: string;
}

/**
 * @description: 点赞操作
 * @param {IFeedBackParams} params
 * @return {*}
 */
export function handleFeedback(params: IFeedBackParams) {
  return Request({
    url: "/api/conversation/feedback",
    method: "PUT",
    data: {
      ...params,
    },
  });
}

interface IGetRecommendedParams {
  deviceType: keyof deviceType;
  num: number;
  userId: string;
}
export function getRecommended(params: IGetRecommendedParams) {
  return Request({
    url: "/api/conversation/getRecommended",
    method: "POST",
    data: {
      data: {
        ...params,
      },
    },
  });
}

export function fetchSessions() {
  return Request({
    url: "/api/session/sessions",
    method: "POST",
    data: {
      requestBase: {
        page: "1-1000",
        sort: "-createTime",
      },
    },
  });
}

interface IFetchAddSessionParams {
  sessionType?: string;
}
export function fetchAddSession({
  sessionType = "text_chat",
}: IFetchAddSessionParams) {
  return Request({
    url: "/api/session/addSession",
    method: "POST",
    data: {
      data: {
        sessionType,
      },
    },
  });
}

export function fetchDelSession(id: string) {
  return Request({
    url: `/api/session/delSession/${id}`,
    method: "DELETE",
  });
}

interface IFetchUpdateSessionParams {
  id: string;
  summary: string;
}
export function fetchUpdateSession(params: IFetchUpdateSessionParams) {
  return Request({
    url: `/api/session/updateSession`,
    method: "PUT",
    data: {
      data: {
        ...params,
      },
    },
  });
}

export function fetchMessages(id: string) {
  return Request({
    url: `/api/session/messages`,
    method: "POST",
    data: {
      data: {
        sessionId: id,
      },
    },
  });
}

interface IFetchLoginParams {
  password: string;
  username: string;
  remember: boolean;
  agree: boolean;
}
export function fetchLogin(params: IFetchLoginParams) {
  return Request({
    url: `/system-manager/umsAdmin/login`,
    method: "POST",
    type: "app",
    data: {
      data: {
        ...params,
      },
    },
  });
}
export function fetchUserInfo() {
  return Request({
    url: `/system-manager/umsAdmin/info`,
    method: "GET",
    type: "app",
  });
}
function createFormData(params = {}, boundary = "") {
  let result = "";
  for (let i in params) {
    result += `\r\n--${boundary}`;
    result += `\r\nContent-Disposition: form-data; name="${i}"`;
    result += "\r\n";
    result += `\r\n${params[i]}`;
  }
  // 如果obj不为空，则最后一行加上boundary
  if (result) {
    result += `\r\n--${boundary}`;
  }
  return result;
}

export async function fetchRefreshToken() {
  let refreshToken = Taro.getStorageSync("refreshToken");
  if (refreshToken) {
    const url = `${app_baseUrl}/system-manager/umsAdmin/refreshToken`;
    const params = {
      refreshToken: refreshToken.replace(/^"|"$/g, ""),
    };
    const boundary = `----FooBar${new Date().getTime()}`;
    const formData = createFormData(params, boundary);

    const reRes: any = await Taro.request({
      url,
      method: "POST",
      header: {
        Accept: "application/json",
        "Content-Type": `multipart/form-data; boundary=${boundary}`,
      },
      data: formData,
    });
    if (reRes.data && reRes.data.isSuccess) {
      Taro.setStorageSync("token", reRes.data.content.token);
      Taro.setStorageSync("refreshToken", reRes.data.content.refreshToken);
      Taro.setStorageSync("tokenHead", reRes.data.content.tokenHead);
      Taro.setStorageSync("tokenHeader", reRes.data.content.tokenHeader);
      return true;
    } else {
      return false;
    }
  }
  return false;
}
